package com.agan.aganjavaspyder.crawler.un_demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cglovewx
 */
@PipelineName("JsonPhontPipeline")
@Slf4j
public class JsonPhontPipeline implements Pipeline<PhoneDto> {

	@Override
	public void process(PhoneDto bean) {
		//解析js对象
		String resultText = parseString(bean.getResultText());
		//入库对象
		JSONObject proList = JSONUtil.parseObj(resultText);
		Map<String,List<Object>> objectMap = proList.toBean(Map.class);
		for (Map.Entry<String,List<Object>> entry:objectMap.entrySet()){
			System.out.println("999999----"+ entry.getValue().get(0));
			JSONObject jsonObject = JSONUtil.parseObj(entry.getValue().get(0));
			Map<String,String> map = jsonObject.toBean(Map.class);
			String color = map.get("COLOR_DESC");
			System.out.println(color);
		}
	}


	/**
	 * 处理初始化数据成可用数据
	 * @return
	 */
	private static String parseString(String result){
		//解析数据
		String resultText = result;
		System.out.println("resultText-----:"+resultText);
		//正哲过滤 换行符号等
		resultText = StrUtil.removeAllLineBreaks(resultText);
		//找到resultInfoObj 之后的
		resultText = StrUtil.subAfter(resultText,"var allRom = ",false);
		//找到resultInfoObj 和;	var resultInfo = JSON.stringify(resultInfoObj); 之间的数据
		resultText = StrUtil.subBefore(resultText,";",false);
		//System.out.println("resultText:"+resultText);
		return resultText;
	}


	public static void main(String[] args) {
		String  resultText ="\n" +
				"    var allColor = {\"2020101404728551\":[{\"COLOR_CODE\":\"1112081707539899\",\"COLOR_DESC\":\"黑色\"},{\"COLOR_CODE\":\"1112081707539907\",\"COLOR_DESC\":\"白色\"},{\"COLOR_CODE\":\"1112081707539911\",\"COLOR_DESC\":\"蓝色\"},{\"COLOR_CODE\":\"1112081707539909\",\"COLOR_DESC\":\"红色\"},{\"COLOR_CODE\":\"1717101379659145\",\"COLOR_DESC\":\"绿色\"}],\"2020101404725663\":[{\"COLOR_CODE\":\"1112081707539899\",\"COLOR_DESC\":\"黑色\"},{\"COLOR_CODE\":\"1112081707539907\",\"COLOR_DESC\":\"白色\"},{\"COLOR_CODE\":\"1112081707539911\",\"COLOR_DESC\":\"蓝色\"},{\"COLOR_CODE\":\"1112081707539909\",\"COLOR_DESC\":\"红色\"},{\"COLOR_CODE\":\"1717101379659145\",\"COLOR_DESC\":\"绿色\"}],\"2020101404721895\":[{\"COLOR_CODE\":\"1112081707539899\",\"COLOR_DESC\":\"黑色\"},{\"COLOR_CODE\":\"1112081707539907\",\"COLOR_DESC\":\"白色\"},{\"COLOR_CODE\":\"1112081707539911\",\"COLOR_DESC\":\"蓝色\"},{\"COLOR_CODE\":\"1112081707539909\",\"COLOR_DESC\":\"红色\"},{\"COLOR_CODE\":\"1717101379659145\",\"COLOR_DESC\":\"绿色\"}]};\n" +
				"    var allRom = {\"2020101404721895^1112081707539899\":[{\"MODEL_DESC\":\"iPhone12 256GB\",\"COLOR_CODE\":\"1112081707539899\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION\":\"2020101404721895\",\"TERM_PRICE\":7599,\"MACHINE_TYPE_ID\":\"7403005125\",\"MACHINE_VERSION_DESC\":\"256GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"黑色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"黑色\",\"SHOW_NAME\":\"256GB\",\"SORT_INDEX\":15}],\"2020101404725663^1112081707539909\":[{\"MODEL_DESC\":\"iPhone12 128GB\",\"COLOR_CODE\":\"1112081707539909\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION\":\"2020101404725663\",\"TERM_PRICE\":6799,\"MACHINE_TYPE_ID\":\"7403005122\",\"MACHINE_VERSION_DESC\":\"128GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"红色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"红色\",\"SHOW_NAME\":\"128GB\",\"SORT_INDEX\":7}],\"2020101404728551^1717101379659145\":[{\"MODEL_DESC\":\"iPhone12 64GB\",\"COLOR_CODE\":\"1717101379659145\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION\":\"2020101404728551\",\"TERM_PRICE\":6299,\"MACHINE_TYPE_ID\":\"7403005119\",\"MACHINE_VERSION_DESC\":\"64GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"绿色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"绿色\",\"SHOW_NAME\":\"64GB\",\"SORT_INDEX\":1}],\"2020101404725663^1112081707539899\":[{\"MODEL_DESC\":\"iPhone12 128GB\",\"COLOR_CODE\":\"1112081707539899\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION\":\"2020101404725663\",\"TERM_PRICE\":6799,\"MACHINE_TYPE_ID\":\"7403005120\",\"MACHINE_VERSION_DESC\":\"128GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"黑色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"黑色\",\"SHOW_NAME\":\"128GB\",\"SORT_INDEX\":10}],\"2020101404728551^1112081707539911\":[{\"MODEL_DESC\":\"iPhone12 64GB\",\"COLOR_CODE\":\"1112081707539911\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION\":\"2020101404728551\",\"TERM_PRICE\":6299,\"MACHINE_TYPE_ID\":\"7403005118\",\"MACHINE_VERSION_DESC\":\"64GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"蓝色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"蓝色\",\"SHOW_NAME\":\"64GB\",\"SORT_INDEX\":4}],\"2020101404728551^1112081707539907\":[{\"MODEL_DESC\":\"iPhone12 64GB\",\"COLOR_CODE\":\"1112081707539907\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION\":\"2020101404728551\",\"TERM_PRICE\":6299,\"MACHINE_TYPE_ID\":\"7403005116\",\"MACHINE_VERSION_DESC\":\"64GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"白色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"白色\",\"SHOW_NAME\":\"64GB\",\"SORT_INDEX\":3}],\"2020101404721895^1112081707539911\":[{\"MODEL_DESC\":\"iPhone12 256GB\",\"COLOR_CODE\":\"1112081707539911\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION\":\"2020101404721895\",\"TERM_PRICE\":7599,\"MACHINE_TYPE_ID\":\"7403005128\",\"MACHINE_VERSION_DESC\":\"256GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"蓝色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"蓝色\",\"SHOW_NAME\":\"256GB\",\"SORT_INDEX\":14}],\"2020101404728551^1112081707539899\":[{\"MODEL_DESC\":\"iPhone12 64GB\",\"COLOR_CODE\":\"1112081707539899\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION\":\"2020101404728551\",\"TERM_PRICE\":6299,\"MACHINE_TYPE_ID\":\"7403005115\",\"MACHINE_VERSION_DESC\":\"64GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"黑色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"黑色\",\"SHOW_NAME\":\"64GB\",\"SORT_INDEX\":5}],\"2020101404721895^1717101379659145\":[{\"MODEL_DESC\":\"iPhone12 256GB\",\"COLOR_CODE\":\"1717101379659145\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION\":\"2020101404721895\",\"TERM_PRICE\":7599,\"MACHINE_TYPE_ID\":\"7403005129\",\"MACHINE_VERSION_DESC\":\"256GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"绿色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"绿色\",\"SHOW_NAME\":\"256GB\",\"SORT_INDEX\":11}],\"2020101404721895^1112081707539907\":[{\"MODEL_DESC\":\"iPhone12 256GB\",\"COLOR_CODE\":\"1112081707539907\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION\":\"2020101404721895\",\"TERM_PRICE\":7599,\"MACHINE_TYPE_ID\":\"7403005126\",\"MACHINE_VERSION_DESC\":\"256GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"白色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"白色\",\"SHOW_NAME\":\"256GB\",\"SORT_INDEX\":13}],\"2020101404725663^1717101379659145\":[{\"MODEL_DESC\":\"iPhone12 128GB\",\"COLOR_CODE\":\"1717101379659145\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION\":\"2020101404725663\",\"TERM_PRICE\":6799,\"MACHINE_TYPE_ID\":\"7403005124\",\"MACHINE_VERSION_DESC\":\"128GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"绿色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"绿色\",\"SHOW_NAME\":\"128GB\",\"SORT_INDEX\":6}],\"2020101404728551^1112081707539909\":[{\"MODEL_DESC\":\"iPhone12 64GB\",\"COLOR_CODE\":\"1112081707539909\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION\":\"2020101404728551\",\"TERM_PRICE\":6299,\"MACHINE_TYPE_ID\":\"7403005117\",\"MACHINE_VERSION_DESC\":\"64GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"红色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"红色\",\"SHOW_NAME\":\"64GB\",\"SORT_INDEX\":2}],\"2020101404725663^1112081707539911\":[{\"MODEL_DESC\":\"iPhone12 128GB\",\"COLOR_CODE\":\"1112081707539911\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION\":\"2020101404725663\",\"TERM_PRICE\":6799,\"MACHINE_TYPE_ID\":\"7403005123\",\"MACHINE_VERSION_DESC\":\"128GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"蓝色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"蓝色\",\"SHOW_NAME\":\"128GB\",\"SORT_INDEX\":9}],\"2020101404721895^1112081707539909\":[{\"MODEL_DESC\":\"iPhone12 256GB\",\"COLOR_CODE\":\"1112081707539909\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION\":\"2020101404721895\",\"TERM_PRICE\":7599,\"MACHINE_TYPE_ID\":\"7403005127\",\"MACHINE_VERSION_DESC\":\"256GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"红色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"红色\",\"SHOW_NAME\":\"256GB\",\"SORT_INDEX\":12}],\"2020101404725663^1112081707539907\":[{\"MODEL_DESC\":\"iPhone12 128GB\",\"COLOR_CODE\":\"1112081707539907\",\"BRAND_DESC\":\"Apple\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION\":\"2020101404725663\",\"TERM_PRICE\":6799,\"MACHINE_TYPE_ID\":\"7403005121\",\"MACHINE_VERSION_DESC\":\"128GB\",\"BRAND_CODE\":\"AP\",\"COLOR_DESC\":\"白色\",\"GOODS_ID\":992010147392,\"IP_COLOR\":\"白色\",\"SHOW_NAME\":\"128GB\",\"SORT_INDEX\":8}]};\n" +
				"    var allVersion = [{\"MACHINE_VERSION\":\"2020101404728551\",\"MODEL_CODE\":\"2020101404728551\",\"MACHINE_VERSION_DESC\":\"64GB\"},{\"MACHINE_VERSION\":\"2020101404725663\",\"MODEL_CODE\":\"2020101404725663\",\"MACHINE_VERSION_DESC\":\"128GB\"},{\"MACHINE_VERSION\":\"2020101404721895\",\"MODEL_CODE\":\"2020101404721895\",\"MACHINE_VERSION_DESC\":\"256GB\"}];";

		String result = parseString(resultText);

		System.out.println("------"+result);
		//入库对象
		JSONObject proList = JSONUtil.parseObj(result);
		System.out.println(proList.toString());
		Map<String,List<Object>> objectMap = proList.toBean(Map.class);

		for (Map.Entry<String,List<Object>> entry:objectMap.entrySet()){
			System.out.println("999999----"+ entry.getValue().get(0));
			JSONObject jsonObject = JSONUtil.parseObj(entry.getValue().get(0));
			Map<String,String> map = jsonObject.toBean(Map.class);
			String color = map.get("COLOR_DESC");
			System.out.println(color);

		}


	}


}